import VueRouter from 'vue-router'
import Vue from 'vue'
import Home from '../views/Home'
import About from '../views/About'

Vue.use(VueRouter)
let routes = [
  {
    path: '/home',
    component: Home,
    meta: {
      qq: !23,
      title: 666,
      age: 888
    },
    children: [
      {
        path: '/home/A',
        name: "AAA",
        meta: {
          // 这里的属性全是自定义的
          title: 'AAA',
          permission: 6
        },
        component: () => import(/*webpackChunkName:"A"*/'../componnets/A.vue')
      },
      {
        path: '/home/B/:qqq',
        name: "BBB",
        component: () => import(/*webpackChunkName:"B"*/'../componnets/B.vue')
      }
    ]
  },
  {
    path: '/about',
    meta: {
      // 这里的属性全是自定义的
      title: 'AAA',
      permission: 6
    },
    // beforeEnter: (to, from, next) => {
    //   // ...
    // },
    component: About
  },
  {
    path: '/404',
    component: () => import('../views/404.vue')
  },
  {
    path: '/*',
    redirect: '/404'
  }
]

let router = new VueRouter({
  routes,
  mode: 'history',
  linkActiveClass: 'current'
  /* 
    hash:  onhashchange这个事件 只要hash发生改变  就会触发这个事件
          hash上线啥事没有
    history:  pushState()  replaceState()  + onpopstate事件
          history上线的时候 需要后端设置
  */
})
let userLevel = 5;
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => {
    console.log(err)
    return err
  })
}
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  document.title = to.meta.title || '珠峰';
  if (to.meta.permission > userLevel) {
    console.log(404)
    next()
    setTimeout(() => {
      next('/404')
    }, 0);
  } else {
    console.log('next')
    next()
  }
})
export default router